package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dao.entity.EmployeeDO;
import com.cskaoyan.wordmemorize.dto.admin.EmployeeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.EmployeeCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.EmployeeService;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/employee")
public class AdminEmployeeController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    FileUploadService aliOssStorageService;

    // 管理员获取当前登录的用户信息
    @GetMapping("info")
    public Result<EmployeeDTO> getEmployeeInfo() {
        // 获取当前登录的用户id
        Long employeeId = StpKit.ADMIN.getLoginIdAsLong();

        EmployeeDTO employeeInfo = employeeService.getEmployeeInfo(employeeId);

        return Result.ok(employeeInfo);
    }

    // 管理员添加用户
    @PostMapping("")
    public Result<Object> addUserEmployee(@RequestBody EmployeeCommand command) {
        employeeService.addEmployee(command);
        return Result.ok();
    }

    // 管理员更新用户
    @PutMapping("")
    public Result<Object> updateUserEmployee(@RequestBody EmployeeCommand command) {
        System.out.println(command);
        employeeService.updateEmployee(command);
        return Result.ok();
    }


    // 管理员上传用户头像
    @PostMapping("avatar")
    public Result<String> uploadAvater(@RequestBody MultipartFile file) {
        try {
            return Result.ok(aliOssStorageService.upload(file));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    // 管理员获取用户列表
    @GetMapping("list")
    public Result<PageDTO<EmployeeDTO>> getUserEmployeeList(PageRequest pageQuery) {
        PageDTO<EmployeeDTO> employeeList = employeeService.getEmployeePage(pageQuery);
        return Result.ok(employeeList);
    }

    // 管理员删除用户
    @DeleteMapping("{employeeId}")
    public Result<Object> deleteUserEmployee(@PathVariable Long employeeId) {
        employeeService.delete(employeeId);
        return Result.ok();
    }

    // 管理员删除用户角色
    @DeleteMapping("{employeeId}/role/{roleId}")
    public Result<Object> deleteUserRoleEmployee(@PathVariable Long employeeId, @PathVariable Long roleId) {
        employeeService.deleteRole(employeeId, roleId);
        return Result.ok();
    }
}
